<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RegistrationController
 *
 * @author Jozef Kováč
 */
namespace Acme\MainBundle\Controller;

use Acme\MainBundle\Entity\Pouzivatel;
//use Acme\MainBundle\Entity\PouzivatelSkola;
use Acme\MainBundle\Entity\PouzivatelTrieda;
use Acme\MainBundle\Form\PouzivatelType;
use Acme\MainBundle\Form\PouzivatelTriedaType;
//use Acme\MainBundle\Form\PouzivatelSkolaType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormError;

class RegistrationController extends Controller {
  
    public function indexAction() {

        $pouzivatel = new Pouzivatel();  
        $pouzivatelTrieda = new PouzivatelTrieda();
        //$pouzivatelSkola = new PouzivatelSkola();                      
        
        $pouzivatelForm = $this->createForm(new PouzivatelType(),$pouzivatel);
        $pouzivatelTriedaForm = $this->createForm(new PouzivatelTriedaType(), $pouzivatelTrieda);
        //$pouzivatelSkolaForm = $this->createForm(new PouzivatelSkolaType(), $pouzivatelSkola);
        
        if ($this->getRequest()->isMethod('POST')) {            
            
                $pouzivatelForm->bind($this->getRequest());            
                $pouzivatelTriedaForm->bind($this->getRequest());
                //$pouzivatelSkolaForm->bind($this->getRequest());

                if ($pouzivatelForm->isValid() && $pouzivatelTriedaForm->isValid()/* && $pouzivatelSkolaForm->isValid()*/) {
                    
                    $pouzivatel = $pouzivatelForm->getData(); 
                    $pouzivatelTrieda = $pouzivatelTriedaForm->getData();
                    /* 
                     * vygeneruj heslo uzivatelovi
                     */ 
                    $heslo = $this->generatePassword();
                    $salt = $this->generatePassword(16);
                    $pouzivatel->setHeslo($salt.(sha1($salt.$heslo)));
                                                           
                    /*
                     * posli uzivatelovi mail s novym heslom
                     */
                    if ($this->sendMail($pouzivatel->getEmail(),
                                        $pouzivatel->getMeno()." ".$pouzivatel->getPriezvisko(),
                                        $heslo)){
                    /*    
                     * ked je mail uspesne poslany, uloz objekt do databazy
                     */                                                           
                    
                        return $this->redirect($this->generateUrl('acme_main_registration_success'));
                    }
                    /*
                     * ak poslanie mailu zlyhalo, vygeneruj chybovu hlasku
                     */
                    
                    $pouzivatelForm->addError(new FormError('Nebolo možné zaslať email. Skús sa znovu zaregistrovať.'));                                        
                }            
        }                
        return $this->render('AcmeMainBundle:Default:registration.html.twig', array (
                             'pouzivatelForm' => $pouzivatelForm->createView(),
                             'pouzivatelTriedaForm' => $pouzivatelTriedaForm->createView(),
                             //'pouzivatelSkolaForm' => $pouzivatelSkolaForm->createView(),
                             'errors'=> $pouzivatelForm->getErrors()));      
    }
    
    public function successAction() {
        return $this->render('AcmeMainBundle:Default:registrationSuccess.html.twig');
    }
    
    public function generateAction() {
        return $this->render('AcmeMainBundle:Default:default.html.twig',array('heslo'=>$this->generatePassword()));
    }
    
    private function generatePassword($length = 8) {
        /* 
         * password (a-zA-Z0-9), min length 8 char
         *  
         * salt (a-zA-Z0-9), length 10 char
         */

        $lowercase = "qwertyuiopasdfghjklzxcvbnm";
        $uppercase = "ASDFGHJKLZXCVBNMQWERTYUIOP";
        $numbers = "1234567890";
        $randomCode = "";

        mt_srand(crc32(microtime()));
        $maxLowercase = strlen($lowercase) - 1;
        
        for ($x = 0; $x < abs($length/3); $x++) {
            $randomCode .= $lowercase{mt_rand(0, $maxLowercase)};
        }

        $maxUppercase = strlen($uppercase) - 1;

        for ($x = 0; $x < abs($length/3); $x++) {
            $randomCode .= $uppercase{mt_rand(0, $maxUppercase)};
        }

        $maxNumbers = strlen($numbers) - 1;
        for ($x = 0; $x < abs($length/3); $x++) {
            $randomCode .= $numbers{mt_rand(0, $maxNumbers)};
        }

        return str_shuffle($randomCode);

    }
    
    private function sendMail($address,$name,$password){
        $message = \Swift_Message::newInstance()
                ->setSubject('Prístupové údaje - kockacik')
                ->setFrom('no-reply@gjh.sk')
                ->setTo(array($address => $name))
                ->setBody($this->renderView('AcmeMainBundle:Default:registrationMail.html.twig',array(
                                            'name' => $name,
                                            'address' => $address,
                                            'password' => $password
                                            )));
        
        return $this->get('mailer')->send($message);        
    }
        
}
?>
